unit UnitPerfil;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, UnitModelo, ImgList, ActnList, DB, ADODB, ExtCtrls,
  ComCtrls, ToolWin, Grids, DBGrids, SMDBGrid, StdCtrls, Mask, jpeg;

type
  TFormPerfil = class(TFormModelo)
    qryMenu: TADOQuery;
    dsMenu: TDataSource;
    qrySubmenu: TADOQuery;
    dsSubmneu: TDataSource;
    qryDadosPERFIL_CODIGO: TWordField;
    qryDadosPERFIL_DESCRICAO: TStringField;
    GroupBox1: TGroupBox;
    SMDBGrid1: TSMDBGrid;
    GroupBox2: TGroupBox;
    SMDBGrid2: TSMDBGrid;
    qryMenuMENU_CODIGO: TSmallintField;
    qryMenuPERFIL_CODIGO: TWordField;
    qryMenuMENU_TITULO: TStringField;
    qryMenus: TADOQuery;
    qryMenusMENU_CODIGO: TSmallintField;
    qryMenusMENU_TITULO: TStringField;
    qryMenuMenu: TStringField;
    qrySubMenus: TADOQuery;
    qrySubMenusINTERNA_CODIGO: TWordField;
    qrySubMenusINTERNA_DESCRICAO: TStringField;
    qrySubMenusMENU_CODIGO: TSmallintField;
    qrySubmenuMENU_CODIGO: TSmallintField;
    qrySubmenuINTERNA_CODIGO: TWordField;
    qrySubmenuPERFIL_CODIGO: TWordField;
    dsSubMenus: TDataSource;
    qrySubmenuInterna: TStringField;
    procedure qryDadosAfterOpen(DataSet: TDataSet);
    procedure qryDadosBeforeClose(DataSet: TDataSet);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure qryMenuAfterOpen(DataSet: TDataSet);
    procedure qryMenuBeforeClose(DataSet: TDataSet);
    procedure qryDadosBeforeOpen(DataSet: TDataSet);
    procedure qryDadosAfterClose(DataSet: TDataSet);
    procedure actNovoExecute(Sender: TObject);
    procedure qryMenuAfterInsert(DataSet: TDataSet);
    procedure qrySubmenuAfterInsert(DataSet: TDataSet);
    procedure actAlterarExecute(Sender: TObject);
    procedure qryMenuBeforeOpen(DataSet: TDataSet);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormPerfil: TFormPerfil;

implementation

uses UnitPrincipal, UnitCadPerfil;

{$R *.dfm}

procedure TFormPerfil.qryDadosAfterOpen(DataSet: TDataSet);
begin
  inherited;
  qryMenu.Open;
end;

procedure TFormPerfil.qryDadosBeforeClose(DataSet: TDataSet);
begin
  inherited;
  qryMenu.Close;
end;

procedure TFormPerfil.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  FormPerfil := nil;
end;

procedure TFormPerfil.qryMenuAfterOpen(DataSet: TDataSet);
begin
  inherited;
  qrySubMenus.Open;
  qrySubmenu.Open;
end;

procedure TFormPerfil.qryMenuBeforeClose(DataSet: TDataSet);
begin
  inherited;
  qrySubmenu.Close;
  qrySubMenus.Close;
end;

procedure TFormPerfil.qryDadosBeforeOpen(DataSet: TDataSet);
begin
  inherited;
  qryMenus.Open;
end;

procedure TFormPerfil.qryDadosAfterClose(DataSet: TDataSet);
begin
  inherited;
  qryMenus.Close;
  qrySubMenus.Close;
end;

procedure TFormPerfil.actNovoExecute(Sender: TObject);
begin
  inherited;
  FormPrincipal.cnRegente.BeginTrans;
  qryDados.Insert;
  qryGeral.SQL.Text := 'SELECT MAX(PERFIL_CODIGO)+1 AS CODIGO FROM PERFIL';
  qryGeral.Open;
  qryDadosPERFIL_CODIGO.Value := qryGeral.Fields.Fields[0].Value;
  if FormCadPerfil = nil then
    FormCadPerfil := TFormCadPerfil.Create(Self);
  if FormCadPerfil.ShowModal = mrOk then
    FormPrincipal.cnRegente.CommitTrans
  else
  begin
    FormPrincipal.cnRegente.RollbackTrans;
    qryDados.Refresh;
  end;
end;

procedure TFormPerfil.qryMenuAfterInsert(DataSet: TDataSet);
begin
  inherited;
  if qryDados.State = dsinsert then
    qryDados.Post;
  if not(qryMenu.State in [dsinsert,dsEdit]) then
    qryMenu.Edit;
  qryMenuPERFIL_CODIGO.Value := qryDadosPERFIL_CODIGO.Value;
end;

procedure TFormPerfil.qrySubmenuAfterInsert(DataSet: TDataSet);
begin
  inherited;
  if qryMenu.State = dsinsert then
    qryMenu.Post;
  if not(qrySubMenu.State in [dsinsert,dsEdit]) then
    qrySubMenu.Edit;
  qrySubMenuPERFIL_CODIGO.Value := qryMenuPERFIL_CODIGO.Value;
  qrySubMenuMENU_CODIGO.Value := qryMenuMENU_CODIGO.Value;
end;

procedure TFormPerfil.actAlterarExecute(Sender: TObject);
begin
  inherited;
  FormPrincipal.cnRegente.BeginTrans;
  qryDados.Edit;
  if FormCadPerfil = nil then
    FormCadPerfil := TFormCadPerfil.Create(Self);
  if FormCadPerfil.ShowModal = mrOk then
    FormPrincipal.cnRegente.CommitTrans
  else
  begin
    FormPrincipal.cnRegente.RollbackTrans;
    qryDados.Refresh;
  end;
end;

procedure TFormPerfil.qryMenuBeforeOpen(DataSet: TDataSet);
begin
  inherited;
  qrySubMenus.Open;
end;

end.
